package com.ruoyi.web.controller.app;

import cn.wanghaomiao.seimi.struct.Request;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;



import com.ruoyi.common.utils.http.HttpUtil;

import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.HgtLog;
import com.ruoyi.system.domain.HgtLogsum;
import com.ruoyi.system.domain.Hgtcrawlerssum;
import com.ruoyi.system.domain.hgtRecords;
import com.ruoyi.system.service.TestService;
import com.ruoyi.web.controller.crawlers.Basic;
import org.seimicrawler.xpath.JXDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/crawlers")
public class TestcrawlersController {
    @Autowired
    private TestService testService;
    @GetMapping("getdata")
    String getdata() {
        String url="https://www.jxsggzy.cn/XZinterface/rest/esinteligentsearch/getFullTextDataNew";

      Date date=new Date();;
        long time = date.getTime();
        //定义一天的时间戳86400000
        long day=86400000;
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str3 = sdf3.format(date);
        String time2=sdf3.format(new Date(time-day));
         //String start_time = "2023-10-01 00:00:00";
         //String end_time = "2024-01-28 00:00:00";
        String start_time = time2;
        String end_time = str3;
        String  str ="{\n" +
                "    \"token\": \"\",\n" +
                "    \"pn\": 0,\n" +
                "    \"rn\": 100,\n" +
                "    \"sdt\": \"\",\n" +
                "    \"edt\": \"\",\n" +
                "    \"wd\": \"\",\n" +
                "    \"inc_wd\": \"\",\n" +
                "    \"exc_wd\": \"\",\n" +
                "    \"fields\": \"\",\n" +
                "    \"cnum\": \"\",\n" +
                "    \"sort\": \"{\\\"webdate\\\":\\\"0\\\",\\\"id\\\":\\\"0\\\"}\",\n" +
                "    \"ssort\": \"\",\n" +
                "    \"cl\": 10000,\n" +
                "    \"terminal\": \"\",\n" +
                "    \"condition\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"categoryname\",\n" +
                "            \"equal\": \"采购意向\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        },\n" +
                "        {\n" +
                "            \"fieldName\": \"xiaqucode\",\n" +
                "            \"equal\": \"360126\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        }\n" +
                "    ],\n" +
                "    \"time\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"webdate\",\n" +
                "            \"startTime\": \""+start_time+"\",\n" +
                "            \"endTime\": \""+end_time+"\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"highlights\": \"\",\n" +
                "    \"statistics\": null,\n" +
                "    \"unionCondition\": [],\n" +
                "    \"accuracy\": \"\",\n" +
                "    \"noParticiple\": \"1\",\n" +
                "    \"searchRange\": null,\n" +
                "    \"noWd\": true\n" +
                "}";
        System.out.println(">>>>>>>>>>>>>>"+str);
        JSONObject requesObject = JSON.parseObject(str);

        String aa = HttpUtil.post(url, str);
        JSONObject jsonObject = JSON.parseObject(aa);

        String result = jsonObject.get("result").toString();
        Integer count = (Integer) JSON.parseObject(result).get("totalcount");
        Object records = JSON.parseObject(result).get("records");
        JSONArray temp=JSON.parseArray(records.toString());
        saverecords(temp);
        System.out.println(">>>>>>>>>>>>>>"+count);
        int page = count / 100+1;
        for(int i=1;i<page;i++){
            requesObject.put("pn",i*100);
            System.out.println(requesObject.toString());
            String bb = HttpUtil.post(url, requesObject.toString());
            JSONObject jsonObject1 = JSON.parseObject(bb);
            String result1 = jsonObject.get("result").toString();
            Object records1 = JSON.parseObject(result).get("records");
            JSONArray temp1=JSON.parseArray(records1.toString());
            //saverecords(temp1);
        }


        return "111";
    }
    public String saverecords(JSONArray temp){
        String ee =
                "系统,信息化,平台,5G,FTTH,IDC,IMS,IPV6,MDM,NB-IoT,NB-IOT,OA,POC,VPDN,VR,WLAN,办公平台,办公系统,办公自动化,标准化考场,标准化考点,兵团手机报,不停车,彩铃,超限,车联网,城域网,大数据,党建,地网,等级保护,电力采集卡平台,电路,短彩信集成,对讲,法务通,赣服通,高校团务通,工业互联网,工作手机,骨干线路,固话,光纤链租赁,光纤租赁,国土资源,过境短信,和宝贝,和管家,和密话,和校园,河长制,呼叫中心,互联网专线,环保监控,会议助理,基础电信,集团V网,集团通讯录," +
                "监控,检务通,交换机直连,教育平台,教育云,精准扶贫,警务通,警眼,敬老关怀,卡口,考场,考试查询,空气监测,宽带,链路,林长制,流量,旅游名片,名厨亮灶,农信通,平安乡村,平台建设,千里眼,区间测速,全信通平台,人居环境,容灾备份,软件开发集成,商户管家,十户联防,食品溯源,视频会议,视频监控,数据通讯,数据专线,数字车间,数字工厂,数字化,数字化校园,水质监测,司法矫正," +
                "司法通,天网,外勤管家,网络安全,网络改造,网络教学,网络链路租赁,尾气排放,卫星通讯,污染监测,污染源,WIFI,无线商话,无线网络,物联网,线路,小区短信,校讯通,校园安防,校园网络,行业手机报,雪亮工程,一键报警,一卡通,医疗云,移动OA,移动办公,移动执法,应急指挥,语音专线,预约诊疗,云存储,云平台,云视讯,灾备,长效管护,政府服务,政府热线," +
                "政务服务,政务管理,政务云,治超,智赣,智赣119,智慧安防,智慧,智能,专网,专线,自然资源,综合布线,综治视联网,宽带,专线";
        System.out.println(">>>>>>>>>>>>"+ee);
    String[] keywords=ee.split(",");
        for(int i=0;i<temp.size();i++){
            hgtRecords aa= JSONObject.parseObject(temp.get(i).toString(), hgtRecords.class);
            JXDocument doc = JXDocument.create(HttpUtils.sendGet("https://www.jxsggzy.cn"+aa.getLinkurl()));
            List cc = doc.sel("//table/tbody/tr");
            JXDocument doc1 = JXDocument.create(cc.get(1).toString());
            List<Object> spanList = doc1.sel("//span//text()");

            String projectName = spanList.get(0).toString();
            String name2 = spanList.get(1).toString();
            String budget = spanList.get(2).toString();
            String expect = spanList.get(3).toString();
            String iskey = "否";
            for(int j=0;j<keywords.length;j++){
                if(name2.contains(keywords[j])||budget.contains(keywords[j])||expect.contains(keywords[j])||projectName.contains(keywords[j])){
                    iskey="是";
                }
            }
            aa.setProjectName(projectName);
            aa.setName2(name2);
            aa.setBudget(budget);
            aa.setExpect(expect);
            aa.setIskey(iskey);
            aa.setType("采购意向");
            testService.saveTransaction(aa);
        }

        return "ok";
    }
    @GetMapping("getdata2")
    String getdata2() {
        String url="https://www.jxsggzy.cn/XZinterface/rest/esinteligentsearch/getFullTextDataNew";

        Date date=new Date();;
        long time = date.getTime();
        //定义一天的时间戳86400000
        long day=86400000;
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str3 = sdf3.format(date);
        String time2=sdf3.format(new Date(time-day));
         //String start_time = "2023-10-01 00:00:00";
         //String end_time = "2024-01-28 00:00:00";
        String start_time = time2;
        String end_time = str3;
        String  str ="{\n" +
                "    \"token\": \"\",\n" +
                "    \"pn\": 0,\n" +
                "    \"rn\": 100,\n" +
                "    \"sdt\": \"\",\n" +
                "    \"edt\": \"\",\n" +
                "    \"wd\": \"\",\n" +
                "    \"inc_wd\": \"\",\n" +
                "    \"exc_wd\": \"\",\n" +
                "    \"fields\": \"\",\n" +
                "    \"cnum\": \"\",\n" +
                "    \"sort\": \"{\\\"webdate\\\":\\\"0\\\",\\\"id\\\":\\\"0\\\"}\",\n" +
                "    \"ssort\": \"\",\n" +
                "    \"cl\": 10000,\n" +
                "    \"terminal\": \"\",\n" +
                "    \"condition\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"categoryname\",\n" +
                "            \"equal\": \"采购公告\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        },\n" +
                "        {\n" +
                "            \"fieldName\": \"xiaqucode\",\n" +
                "            \"equal\": \"360126\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        }\n" +
                "    ],\n" +
                "    \"time\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"webdate\",\n" +
                "            \"startTime\": \""+start_time+"\",\n" +
                "            \"endTime\": \""+end_time+"\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"highlights\": \"\",\n" +
                "    \"statistics\": null,\n" +
                "    \"unionCondition\": [],\n" +
                "    \"accuracy\": \"\",\n" +
                "    \"noParticiple\": \"1\",\n" +
                "    \"searchRange\": null,\n" +
                "    \"noWd\": true\n" +
                "}";
        System.out.println(">>>>>>>>>>>>>>"+str);
        JSONObject requesObject = JSON.parseObject(str);

        String aa = HttpUtil.post(url, str);
        JSONObject jsonObject = JSON.parseObject(aa);

        String result = jsonObject.get("result").toString();
        Integer count = (Integer) JSON.parseObject(result).get("totalcount");
        Object records = JSON.parseObject(result).get("records");
        JSONArray temp=JSON.parseArray(records.toString());
        saverecords2(temp);
        System.out.println(">>>>>>>>>>>>>>"+count);
        int page = count / 100+1;
        for(int i=1;i<page;i++){
            requesObject.put("pn",i*100);
            System.out.println(requesObject.toString());
            String bb = HttpUtil.post(url, requesObject.toString());
            JSONObject jsonObject1 = JSON.parseObject(bb);
            String result1 = jsonObject.get("result").toString();
            Object records1 = JSON.parseObject(result).get("records");
            JSONArray temp1=JSON.parseArray(records1.toString());
            //saverecords(temp1);
        }


        return "111";
    }
    public String saverecords2(JSONArray temp){
        String ee =
                "系统,信息化,平台,5G,FTTH,IDC,IMS,IPV6,MDM,NB-IoT,NB-IOT,OA,POC,VPDN,VR,WLAN,办公平台,办公系统,办公自动化,标准化考场,标准化考点,兵团手机报,不停车,彩铃,超限,车联网,城域网,大数据,党建,地网,等级保护,电力采集卡平台,电路,短彩信集成,对讲,法务通,赣服通,高校团务通,工业互联网,工作手机,骨干线路,固话,光纤链租赁,光纤租赁,国土资源,过境短信,和宝贝,和管家,和密话,和校园,河长制,呼叫中心,互联网专线,环保监控,会议助理,基础电信,集团V网,集团通讯录," +
                        "监控,检务通,交换机直连,教育平台,教育云,精准扶贫,警务通,警眼,敬老关怀,卡口,考场,考试查询,空气监测,宽带,链路,林长制,流量,旅游名片,名厨亮灶,农信通,平安乡村,平台建设,千里眼,区间测速,全信通平台,人居环境,容灾备份,软件开发集成,商户管家,十户联防,食品溯源,视频会议,视频监控,数据通讯,数据专线,数字车间,数字工厂,数字化,数字化校园,水质监测,司法矫正," +
                        "司法通,天网,外勤管家,网络安全,网络改造,网络教学,网络链路租赁,尾气排放,卫星通讯,污染监测,污染源,WIFI,无线商话,无线网络,物联网,线路,小区短信,校讯通,校园安防,校园网络,行业手机报,雪亮工程,一键报警,一卡通,医疗云,移动OA,移动办公,移动执法,应急指挥,语音专线,预约诊疗,云存储,云平台,云视讯,灾备,长效管护,政府服务,政府热线," +
                        "政务服务,政务管理,政务云,治超,智赣,智赣119,智慧安防,智慧,智能,专网,专线,自然资源,综合布线,综治视联网,宽带,专线";
        System.out.println(">>>>>>>>>>>>"+ee);
        String[] keywords=ee.split(",");
        for(int i=0;i<temp.size();i++){
            hgtRecords aa= JSONObject.parseObject(temp.get(i).toString(), hgtRecords.class);
            JXDocument doc = JXDocument.create(HttpUtils.sendGet("https://www.jxsggzy.cn"+aa.getLinkurl()));
            List cc = doc.sel("//table/tbody/tr");
            JXDocument doc1 = JXDocument.create(cc.get(1).toString());
            List<Object> spanList = doc1.sel("//span//text()");

            String projectName = spanList.get(0).toString();
            String name2 = spanList.get(1).toString();
            String budget = spanList.get(4).toString().replaceAll("元","");
            String expect = "";
            String iskey = "否";
            for(int j=0;j<keywords.length;j++){
                if(name2.contains(keywords[j])||budget.contains(keywords[j])||expect.contains(keywords[j])||projectName.contains(keywords[j])){
                    iskey="是";
                }
            }
            aa.setProjectName(projectName);
            aa.setName2(name2);
            aa.setBudget(budget);
            aa.setExpect(expect);
            aa.setIskey(iskey);
            aa.setType("采购公告");
            testService.saveTransaction(aa);
        }

        return "ok";
    }
    @GetMapping("getdata3")
    String getdata3() {
        String url="https://www.jxsggzy.cn/XZinterface/rest/esinteligentsearch/getFullTextDataNew";

        Date date=new Date();;
        long time = date.getTime();
        //定义一天的时间戳86400000
        long day=86400000;
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str3 = sdf3.format(date);
        String time2=sdf3.format(new Date(time-day));
        //String start_time = "2023-10-01 00:00:00";
        //String end_time = "2024-01-28 00:00:00";
        String start_time = time2;
        String end_time = str3;
        String  str ="{\n" +
                "    \"token\": \"\",\n" +
                "    \"pn\": 0,\n" +
                "    \"rn\": 100,\n" +
                "    \"sdt\": \"\",\n" +
                "    \"edt\": \"\",\n" +
                "    \"wd\": \"\",\n" +
                "    \"inc_wd\": \"\",\n" +
                "    \"exc_wd\": \"\",\n" +
                "    \"fields\": \"\",\n" +
                "    \"cnum\": \"\",\n" +
                "    \"sort\": \"{\\\"webdate\\\":\\\"0\\\",\\\"id\\\":\\\"0\\\"}\",\n" +
                "    \"ssort\": \"\",\n" +
                "    \"cl\": 10000,\n" +
                "    \"terminal\": \"\",\n" +
                "    \"condition\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"categoryname\",\n" +
                "            \"equal\": \"中标公示\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        },\n" +
                "        {\n" +
                "            \"fieldName\": \"xiaqucode\",\n" +
                "            \"equal\": \"360126\",\n" +
                "            \"notEqual\": null,\n" +
                "            \"equalList\": null,\n" +
                "            \"notEqualList\": null,\n" +
                "            \"isLike\": true,\n" +
                "            \"likeType\": 2\n" +
                "        }\n" +
                "    ],\n" +
                "    \"time\": [\n" +
                "        {\n" +
                "            \"fieldName\": \"webdate\",\n" +
                "            \"startTime\": \""+start_time+"\",\n" +
                "            \"endTime\": \""+end_time+"\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"highlights\": \"\",\n" +
                "    \"statistics\": null,\n" +
                "    \"unionCondition\": [],\n" +
                "    \"accuracy\": \"\",\n" +
                "    \"noParticiple\": \"1\",\n" +
                "    \"searchRange\": null,\n" +
                "    \"noWd\": true\n" +
                "}";
        System.out.println(">>>>>>>>>>>>>>"+str);
        JSONObject requesObject = JSON.parseObject(str);

        String aa = HttpUtil.post(url, str);
        JSONObject jsonObject = JSON.parseObject(aa);

        String result = jsonObject.get("result").toString();
        Integer count = (Integer) JSON.parseObject(result).get("totalcount");
        Object records = JSON.parseObject(result).get("records");
        JSONArray temp=JSON.parseArray(records.toString());
        saverecords3(temp);
        System.out.println(">>>>>>>>>>>>>>"+count);
        int page = count / 100+1;
        for(int i=1;i<page;i++){
            requesObject.put("pn",i*100);
            System.out.println(requesObject.toString());
            String bb = HttpUtil.post(url, requesObject.toString());
            JSONObject jsonObject1 = JSON.parseObject(bb);
            String result1 = jsonObject.get("result").toString();
            Object records1 = JSON.parseObject(result).get("records");
            JSONArray temp1=JSON.parseArray(records1.toString());
            //saverecords(temp1);
        }


        return "111";
    }
    public String saverecords3(JSONArray temp){
        String ee =
                "系统,信息化,平台,5G,FTTH,IDC,IMS,IPV6,MDM,NB-IoT,NB-IOT,OA,POC,VPDN,VR,WLAN,办公平台,办公系统,办公自动化,标准化考场,标准化考点,兵团手机报,不停车,彩铃,超限,车联网,城域网,大数据,党建,地网,等级保护,电力采集卡平台,电路,短彩信集成,对讲,法务通,赣服通,高校团务通,工业互联网,工作手机,骨干线路,固话,光纤链租赁,光纤租赁,国土资源,过境短信,和宝贝,和管家,和密话,和校园,河长制,呼叫中心,互联网专线,环保监控,会议助理,基础电信,集团V网,集团通讯录," +
                        "监控,检务通,交换机直连,教育平台,教育云,精准扶贫,警务通,警眼,敬老关怀,卡口,考场,考试查询,空气监测,宽带,链路,林长制,流量,旅游名片,名厨亮灶,农信通,平安乡村,平台建设,千里眼,区间测速,全信通平台,人居环境,容灾备份,软件开发集成,商户管家,十户联防,食品溯源,视频会议,视频监控,数据通讯,数据专线,数字车间,数字工厂,数字化,数字化校园,水质监测,司法矫正," +
                        "司法通,天网,外勤管家,网络安全,网络改造,网络教学,网络链路租赁,尾气排放,卫星通讯,污染监测,污染源,WIFI,无线商话,无线网络,物联网,线路,小区短信,校讯通,校园安防,校园网络,行业手机报,雪亮工程,一键报警,一卡通,医疗云,移动OA,移动办公,移动执法,应急指挥,语音专线,预约诊疗,云存储,云平台,云视讯,灾备,长效管护,政府服务,政府热线," +
                        "政务服务,政务管理,政务云,治超,智赣,智赣119,智慧安防,智慧,智能,专网,专线,自然资源,综合布线,综治视联网,宽带,专线";
        System.out.println(">>>>>>>>>>>>"+ee);
        String[] keywords=ee.split(",");
        for(int i=0;i<temp.size();i++){
            hgtRecords aa= JSONObject.parseObject(temp.get(i).toString(), hgtRecords.class);
            JXDocument doc = JXDocument.create(HttpUtils.sendGet("https://www.jxsggzy.cn"+aa.getLinkurl()));
            List cc = doc.sel("//table/tbody/tr");
            if(cc.size()>0) {
                JXDocument doc1 = JXDocument.create(cc.get(1).toString());
                List<Object> spanList = doc1.sel("//span//text()");
                String projectName="";
                String name2="";
                String budget="";
                if(spanList.size()>0) {
                  projectName = spanList.get(0).toString();
                  name2 = spanList.get(0).toString();
                  budget = spanList.get(3).toString().replaceAll("元","");
                }
                String expect = "";
                String iskey = "否";
                for (int j = 0; j < keywords.length; j++) {
                    if (name2.contains(keywords[j]) || budget.contains(keywords[j]) || expect.contains(keywords[j]) || projectName.contains(keywords[j])) {
                        iskey = "是";
                    }
                }
                aa.setProjectName(projectName);
                aa.setName2(name2);
                aa.setBudget(budget);
                aa.setExpect(expect);
                aa.setIskey(iskey);
                aa.setType("中标公示");
                testService.saveTransaction(aa);
            }else{
                System.out.println(">>>>>>>>>>>>>>>>无效中标公示");
            }
        }

        return "ok";
    }
    @PostMapping ("tjsum")
    HashMap<Object, Object> tjsum(String type) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        Hgtcrawlerssum bb = testService.findsum(type);
        DecimalFormat df = new DecimalFormat("#0.00");
        bb.setSum(df.format(Double.parseDouble(bb.getSum())/10000)+"");
        System.out.println(type);
        aa.put("resultData",bb);
        map.put("data",aa);
        return map;
    }
}
